#include "image.h"

extern  uint8_t mt9v03x_image[MT9V03X_UART_H][MT9V03X_UART_W];     
	uint8_t Image_Use[30][40];
 	uint8_t Pixle[30][40];

void Image_Get(void)
{
    unsigned char i, j;
  
    
       
			  for (i = 0; i < 40; i++)
	      {
					 for (j = 0; j < 30; j++)
           Image_Use[j][i] = mt9v03x_image[j*2][i*2];
        }
}

int GetOSTU(unsigned char tmImage[30][40])
{
    signed short i,j;
    unsigned long Amount = 0;
    unsigned long PixelBack = 0;
    unsigned long PixelIntegralBack = 0;
    unsigned long PixelIntegral = 0;
    signed long PixelIntegralFore = 0;
    signed long PixelFore = 0;
    float OmegaBack, OmegaFore, MicroBack, MicroFore, SigmaB, Sigma; // ????;
    signed short MinValue, MaxValue;
    signed short Threshold = 0;
    unsigned char HistoGram[256];              //

    for (j = 0; j < 256; j++)  HistoGram[j] = 0; //????????

    for (j = 0; j < 30; j++)
    {
        for (i = 0; i < 40; i++)
        {
            HistoGram[tmImage[j][i]]++; //???????????????????
        }
    }

    for (MinValue = 0; MinValue < 256 && HistoGram[MinValue] == 0; MinValue++) ;        //????????
    for (MaxValue = 255; MaxValue > MinValue && HistoGram[MinValue] == 0; MaxValue--) ; //????????

    if (MaxValue == MinValue)     return MaxValue;         // ?????????
    if (MinValue + 1 == MaxValue)  return MinValue;        // ?????????

    for (j = MinValue; j <= MaxValue; j++)    Amount += HistoGram[j];        //  ????

    PixelIntegral = 0;
    for (j = MinValue; j <= MaxValue; j++)
    {
        PixelIntegral += HistoGram[j] * j;//?????
    }
    SigmaB = -1;
    for (j = MinValue; j < MaxValue; j++)
    {
        PixelBack = PixelBack + HistoGram[j];   //??????
        PixelFore = Amount - PixelBack;         //??????
        OmegaBack = (float)PixelBack / Amount;//???????
        OmegaFore = (float)PixelFore / Amount;//???????
        PixelIntegralBack += HistoGram[j] * j;  //?????
        PixelIntegralFore = PixelIntegral - PixelIntegralBack;//?????
        MicroBack = (float)PixelIntegralBack / PixelBack;   //???????
        MicroFore = (float)PixelIntegralFore / PixelFore;   //???????
        Sigma = OmegaBack * OmegaFore * (MicroBack - MicroFore) * (MicroBack - MicroFore);//??????
        if (Sigma > SigmaB)                    //?????????g //???????????????
        {
            SigmaB = Sigma;
            Threshold = j;
        }
    }
    return Threshold;                        //??????;
}
void Get_01_Value(void)
{
	int Threshold;
    int i = 0,j = 0;
    Threshold = GetOSTU(Image_Use);//?????

    /* ??? */
    for(i = 0; i < 30; i++)
    {
        for(j = 0; j < 40; j++)
        {
              if(Image_Use[i][j] >Threshold) //????,???????,???????????
                {
									Pixle[i][j] =1;    
                }
              else
                Pixle[i][j] =0;
        }
    }

}
void picture_scan(void)
{ uint8_t h=0,l=0;
 
	for(l=0;l<40;l++)
	{
		for(h=0;h<30;h++)
			{
		
			 // if(Pixle[h][l]==1)
			//	OLED_DrawPoint(l,h);
			//	else
			//	OLED_ClearPoint(l,h);	
			}
	}
	//	OLED_Refresh();
}

